package com.myk.game.lib.dialogs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.myk.game.lib.R;

/**
 * 自定义的加载弹窗
 */
public class LoadingDialog extends Dialog {

    private volatile static LoadingDialog sDialog;

    //默认构造函数
    private LoadingDialog(Context context, CharSequence message) {
        super(context, R.style.CustomLoadingDialog);

        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        TextView tvMessage = view.findViewById(R.id.tv_dialog_loading_message);
        if (!TextUtils.isEmpty(message)) {
            tvMessage.setText(message);
        }
        else {
            tvMessage.setVisibility(View.GONE);
        }
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addContentView(view, lp);
    }

    //阻止Dialog弹出导航栏
    private static void fullScreenImmersive(View view) {
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        view.setSystemUiVisibility(uiOptions);
    }

    //显示LoadingDialog实际方法
    private static void showLoadingDialog(Context context, CharSequence message, boolean cancelable){
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }

        if (!(context instanceof Activity)) {
            return;
        }

        sDialog = new LoadingDialog(context, message);
        sDialog.setCancelable(cancelable);

        if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) {
            sDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            sDialog.show();
            fullScreenImmersive(sDialog.getWindow().getDecorView());
            sDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
    }

    
    /****************************  对外方法  **************************/

    public static void showDialog() {
        showLoadingDialog(ActivityUtils.getTopActivity(),"loading...",false);
    }

    public static void showDialog(CharSequence message) {
        showLoadingDialog(ActivityUtils.getTopActivity(), message,false);
    }

    public static void showDialog(CharSequence message, boolean cancelable) {
        showLoadingDialog(ActivityUtils.getTopActivity(), message, cancelable);
    }

    public static void showDialog(Context context) {
        showLoadingDialog(context, "loading...",false);
    }

    public static void showDialog(Context context, CharSequence message) {
        showLoadingDialog(context, message, false);
    }

    public static void showDialog(Context context, CharSequence message, boolean cancelable) {
        showLoadingDialog(context, message, cancelable);
    }

    public static void closeDialog() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
        sDialog = null;
    }
}